<?php

namespace App\Http\Controllers\Page;

use App\Http\Controllers\Base\Base;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class Table extends Controller
{
    private $baseClass;

    function __construct()
    {
        $this->baseClass = new Base();
    }

    public function loadTable(){
        $arrayData = array();
        if ($this->baseClass->checkLoginStatus() == true){
            $pageView = 'table';
            $arrayData['menuOpen2'] = 'menu-open';
            $arrayData['menuOpen1'] = '';
            $arrayData['active2'] = 'active';
            $arrayData['active1'] = '';
        }
        else {
            $pageView = 'login';
        }
        return view($pageView, $arrayData);
    }

    public function loadTableJson(){
        $data = array();
        if ($this->baseClass->checkLoginStatus() == true){
            $start = $this->baseClass->htmlPurifier('start');
            $perPage = $this->baseClass->htmlPurifier('length');
            $draw = $this->baseClass->htmlPurifier('draw');
            $order = Input::get('order')[0]['dir'];
            $search = $this->baseClass->htmlPurifier('searchName');
            $page = ceil($start / $perPage);
            $page = $page < 1 ? 1 : $page + 1;
            $queryData = array();
            $queryData['search'] = $search;
            $result = DB::table($this->baseClass->recordTable)
                ->orderBy('commit_time', $order)
                ->orderBy('receiver', 'DESC')
                ->select('receiver', 'sender', 'cherry_class', 'commit_time', 'cherry_weight')
                ->where(function ($query) use ($queryData){
                    $search = $queryData['search'];
                    $query->where('receiver', 'like', '%'.$search.'%')
                        ->orWhere('sender', 'like', '%'.$search.'%');
                })
                ->count();
            $data['recordsTotal'] = (int)$result;
            $data['recordsFiltered'] = (int)$result;
            $pageTotal = ceil($result / $perPage);
            if ($page > $pageTotal){
                $page = $pageTotal;
            }
            if ($page == '' || $page <= 0){
                $page = 1;
            }
            $skipNum = ($page - 1) * $perPage;
            $takeNum = $perPage;
            $result = DB::table($this->baseClass->recordTable)
                ->orderBy('commit_time', $order)
                ->orderBy('receiver', 'DESC')
                ->select('receiver', 'sender', 'cherry_class', 'cherry_weight', 'send_time', 'commit_time', 'id')
                ->where(function ($query) use ($queryData){
                    $search = $queryData['search'];
                    $query->where('receiver', 'like', '%'.$search.'%')
                        ->orWhere('sender', 'like', '%'.$search.'%');
                })
                ->skip($skipNum)
                ->take($takeNum)
                ->get();
            $data['draw'] = (int)$draw;
            $data['page'] = (int)$page;
            $data['data'] = array();
            if (count($result) > 0){
                foreach ($result as $item){
                    $array = array();
                    $array['receiver'] = $item->receiver;
                    $array['sender'] = $item->sender;
                    $array['id'] = $item->id;
                    $array['commit_time'] = date('Y-m-d', $item->commit_time);
                    $array['cherry_weight'] = $item->cherry_weight;
                    if ($item->cherry_class == 1) {
                        $array['cherry_class'] = '美早';
                    }
                    else {
                        $array['cherry_class'] = '红灯';
                    }
                    $data['data'][] = $array;
                }
            }
        }
        return json_encode($data);
//        return $this->baseClass->returnJson($status, $content, $data);
    }
}
